package com.ktg.mes.sys.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.mes.sys.mapper.SysLogMapper;
import com.ktg.mes.sys.domain.SysLog;
import com.ktg.mes.sys.service.ISysLogService;



/**
 * 日志记录Service业务层处理
 *
 * @author byteco
 * @date 2023-02-07
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService
{




    /**
     * 查询日志记录
     *
     * @param entity 日志记录
     * @param page 分页对象
     * @return 日志记录
     */
    @Override
    public IPage<SysLog> queryPageList(SysLog entity, Page<SysLog> page) {
        QueryWrapper<SysLog> lqw = new QueryWrapper<SysLog>(entity);
        lqw.orderByDesc("create_time");
        IPage<SysLog> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询日志记录
     *
     * @param entity 日志记录
     * @return 日志记录
     */
    @Override
    public List<SysLog> queryList(SysLog entity) {
        QueryWrapper<SysLog> lqw = new QueryWrapper<SysLog>(entity);
        lqw.orderByDesc("create_time");
        return this.list(lqw);
    }

    public SysLog getInfo(Long id)
    {
        return this.getById(id);
    }


    /**
     * 新增日志记录
     *
     * @param entity 日志记录
     * @return 结果
     */
    @Override
    public void insert(SysLog entity)
    {
        deleteSysLogOnlySaveDay(2);
        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        this.save(entity);
    }

    /**
     * 保留 多少天日志
     * @param dayCount
     */
    public void deleteSysLogOnlySaveDay(Integer dayCount){
        QueryWrapper queryWrapper = new QueryWrapper<SysLog>();
        queryWrapper.le("create_time","DATE_SUB(create_time,INTERVAL "+dayCount+" DAY)");
        getBaseMapper().delete(queryWrapper);
    }


    /**
     * 修改日志记录
     *
     * @param entity 日志记录
     * @return 结果
     */
    @Override
    public boolean update(SysLog entity)
    {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return this.updateById(entity);
    }

    /**
     * 批量删除日志记录
     *
     * @param recordIds 需要删除的日志记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long[] recordIds)
    {

        return this.removeByIds(Arrays.asList(recordIds));
    }

    /**
     * 删除日志记录信息
     *
     * @param recordId 日志记录主键
     * @return 结果
     */
    @Override
    public boolean delete(Long recordId)
    {
        return this.removeById(recordId);
    }

    private LambdaQueryWrapper<SysLog> buildQueryWrapper(SysLog entity) {
        LambdaQueryWrapper<SysLog> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(entity.getType()), SysLog::getType, entity.getType());
                    lqw.eq(StringUtils.isNotBlank(entity.getTitle()), SysLog::getTitle, entity.getTitle());
                    lqw.eq(StringUtils.isNotBlank(entity.getLocation()), SysLog::getLocation, entity.getLocation());
                    lqw.eq(StringUtils.isNotBlank(entity.getParam()), SysLog::getParam, entity.getParam());
                    lqw.eq(StringUtils.isNotBlank(entity.getReslut()), SysLog::getReslut, entity.getReslut());
        return lqw;
    }
}
